package com.xyebank.ledger.service.sjsh.adaper;

import com.xyebank.ledger.db.entitys.LApplicationInf;
import com.xyebank.ledger.service.common.conveter.ApplyBeanConverter;
import com.xyebank.ledger.service.constant.CommonConstant;
import com.xyebank.ledger.service.constant.ProductConstant;
import com.xyebank.ledger.util.DateConverter;
import com.xyebank.ledger.util.DateUtil;
import com.xyebank.ledger.util.IDUtil;
import com.xyebank.ledger.vo.deal.SJSHCashApplyVo;

import org.modelmapper.AbstractConverter;
import org.modelmapper.Converter;
import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component(value = "sjshApplyBeanConverter")
public class SJSHApplyBeanConverter implements ApplyBeanConverter<SJSHCashApplyVo> {

	ModelMapper modelMapper = new ModelMapper();


	// 不能采取注入方式
	private DateConverter dateConverter = new DateConverter();

	class NXApplyPropertyMap extends PropertyMap<SJSHCashApplyVo, LApplicationInf> {
		// 注意 不要有重复的映射，不然会报错
		protected void configure() {
			// 机构订单号
			map(source.getContactNo(), destination.getOrgOrderNo());
			// 电话号码
			map(source.getMblNo(), destination.getMblNo());
			// 申请时间
			using(dateConverter).map(source.getApplTime(), destination.getApplyTime());
			// 授信时间
			map(source.getCreditTime(), destination.getCreditTime());
			// 授信金额（可用金额）
			map(source.getAccLim(), destination.getCreditAmount());
			// 授信额度有效期
;			// 授信未通过原因
			map(source.getRefuseDesc(), destination.getRefuseDesc());
			// 成功绑卡时间
			map(source.getBindCardTime(), destination.getBindCardTime());
			// 绑卡成功结果
			map(source.getBindCardRes(), destination.getBindCardRes());
			// 产品利率
			map(source.getInstRate(), destination.getInstRate());
			// 提现手续费费率
			map(source.getFeeAmt(), destination.getFeeAmt());
			// 状态
//			map(source.getApplStatus(), destination.getStatus());

		}
	}

	public SJSHApplyBeanConverter() {
		modelMapper.addMappings(new NXApplyPropertyMap());
	}

	@Override
	public LApplicationInf covert(SJSHCashApplyVo x) {
		LApplicationInf entity = modelMapper.map(x, LApplicationInf.class);
		entity.setDataSource(x.getChannelNo().equals("51000005") ? CommonConstant.JRY_STR : CommonConstant.SJD_STR);
		entity.setXyOrgNo(ProductConstant.SJSH_LOAN_ORG);
		entity.setXyProductNo(ProductConstant.SJSH_LOAN_PROD_ID);
		entity.setId(IDUtil.generateId(x.getId().toString(), "sjsh_apply"));
		String status = x.getAppralRes();
		if("未通过".equals(status)){
			entity.setStatus(CommonConstant.APPLY_FAIL);
		}else if ("通过".equals(status)) {
			entity.setStatus(CommonConstant.APPLY_SUCCESS);
		}else if ("处理中".equals(status)) {
			entity.setStatus(CommonConstant.APPLY_ING);
		}
		return entity;
	}
}
